package com.org.cloud.lg.handler;
import com.google.common.base.Throwables;
import com.org.cloud.lg.utils.ApiResult;
import com.org.cloud.lg.utils.Checker;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @ClassName: GlobalExceptionHander
 * @Author: LG
 * @Date: 2019/5/22 13:58
 * @Version: 1.0
 **/
@ControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    @ExceptionHandler
    @ResponseBody
    public ApiResult handle(Exception ex) {
        log.error(ex.getMessage());
        ApiResult result = adornerExcepiton(ex);
        return result;
    }

    private ApiResult adornerExcepiton(Exception ex){
        if (CustomException.class.isInstance(ex)) {
            return ((CustomException) ex).getR();
        }
        else{
            String message=Checker.BeNotBlank(ex.getMessage())?ex.getMessage():Throwables.getStackTraceAsString(ex);
            log.error(message);
            return ApiResult.result(-1);
        }
    }
}
